#include "common.h"


ParamBase *ParamBase::mInstance = NULL;

ParamBase::ParamBase(QObject *parent)
    : QObject(parent)
{
    mIsCheckScreenMode = false;
    mLocationIp         = "";
    mCarHead            = DEFAULTVAL;
    mCarLocation        = DEFAULTVAL;
    mCurTrainHeadNum    = DEFAULTVAL;
    mStartStationNum    = DEFAULTVAL;
    mStopStationNum     = DEFAULTVAL;
    mCurStationNum      = DEFAULTVAL;
    mNextStationNum     = DEFAULTVAL;
    mCurDoorOpenSide    = DEFAULTVAL;
    mRunDir             = DEFAULTVAL;
    mDevLocation        = DEFAULTVAL;
    mDoorIsolation      = DEFAULTVAL;
    mLineId             = DEFAULTVAL;
    mpXmlDomEle         = NULL;
    mEmerge = DEFAULTVAL;

    m_ui_type = "";
    m_file_name = "";
    m_start_id = 0;
    m_stop_id = 0;
    m_curr_id = 0;
    m_next_id = 0;
    m_car_number = 0;
    m_door_status = 0;
    m_skip_list.clear();
    m_arrive_time_list.clear();
    m_scroll_msg = "";
    m_emergency_img = "";
    m_emergency_msg_list.clear();
}

ParamBase *ParamBase::getParamBasePtr()
{
    if (mInstance == NULL)
    {
        mInstance = new ParamBase;
    }
    return mInstance;
}

/**
 * @brief ParamBase::~ParamBase
 */
ParamBase::~ParamBase()
{
    if(NULL != mpXmlDomEle && !mpXmlDomEle->isNull())
    {
        delete mpXmlDomEle;
        mpXmlDomEle = NULL;
    }
}

/**
 * @brief ParamBase::UpdateParamBase
 * @param rParamBase
 */
void ParamBase::UpdateParamBase(const ParamBase& rParamBase)
{
    if(DEFAULTVAL != rParamBase.GetCarHead())
        SetCarHead(rParamBase.GetCarHead());

    if(DEFAULTVAL != rParamBase.GetCarLoaction())
        SetCarLoaction(rParamBase.GetCarLoaction());

    if(DEFAULTVAL != rParamBase.GetCurTrainHeadNum())
        SetCurTrainHeadNum(rParamBase.GetCurTrainHeadNum());
    if(DEFAULTVAL != rParamBase.GetStartStationNum())
        SetStartStationNum(rParamBase.GetStartStationNum());
    if(DEFAULTVAL != rParamBase.GetStopStationNum())
        SetStopStationNum(rParamBase.GetStopStationNum());
    if(DEFAULTVAL != rParamBase.GetCurStationNum())
        SetCurStationNum(rParamBase.GetCurStationNum());
    if(DEFAULTVAL != rParamBase.GetNextStationNum())
        SetNextStationNum(rParamBase.GetNextStationNum());
    if(DEFAULTVAL != rParamBase.GetCurDoorOpenSide())
        SetCurDoorOpenSide(rParamBase.GetCurDoorOpenSide());
    if(DEFAULTVAL != rParamBase.GetLineId())
        SetLineId(rParamBase.GetLineId());
    if(DEFAULTVAL != rParamBase.GetRunDir())
        SetRunDir(rParamBase.GetRunDir());
    if(DEFAULTVAL != rParamBase.GetDevLoaction())
        SetDevLoaction(rParamBase.GetDevLoaction());

    if(DEFAULTVAL != rParamBase.GetDoorIsolation())
        SetDoorIsolation(rParamBase.GetDoorIsolation());

    if(DEFAULTVAL != rParamBase.GetEmergeId())
        SetEmergeId(rParamBase.GetEmergeId());

        SetLoactionIp(rParamBase.GetLoactionIp());


//    if(NULL != rParamBase.GetXmlDomEle())
//    {
//        LOG_QDEBUG << __FUNCTION__ << "delete mpXmlDomEle" << rParamBase.GetXmlDomEle();
//        if(NULL != mpXmlDomEle)delete mpXmlDomEle;
//        LOG_QDEBUG << __FUNCTION__ << "new mpXmlDomEle";
//        mpXmlDomEle = new QDomElement(*(rParamBase.mpXmlDomEle));
//        LOG_QDEBUG << __FUNCTION__ << "new mpXmlDomEle end";
//    }

}

/**
 * @brief ParamBase::IsParamNeedUpdate
 * @param rParamBase
 * @return
 */

bool ParamBase::IsParamEmergency(const ParamBase& rParamBase)
{
    if(this->GetEmergeId() != rParamBase.GetEmergeId())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetEmergeId" << this->GetEmergeId() << rParamBase.GetEmergeId();
        return true;
    }
    return false;
}

bool ParamBase::IsParamNeedUpdate(const ParamBase& rParamBase)
{
    if(this->GetCarHead() != rParamBase.GetCarHead())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetCarHead" << this->GetCarHead() << rParamBase.GetCarHead();
        return true;
    }


    if(this->GetCarLoaction() != rParamBase.GetCarLoaction())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetCarLoaction";
        return true;
    }

    if(this->GetCurTrainHeadNum() != rParamBase.GetCurTrainHeadNum())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetCurTrainHeadNum";
        return true;
    }

    if(this->GetStartStationNum() != rParamBase.GetStartStationNum())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetStartStationNum";
        return true;
    }

    if(this->GetStopStationNum() != rParamBase.GetStopStationNum())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetStopStationNum";
        return true;
    }

    if(this->GetCurStationNum() != rParamBase.GetCurStationNum())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetCurStationNum";
        return true;
    }

    if(this->GetNextStationNum() != rParamBase.GetNextStationNum())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetNextStationNum";
        return true;
    }

    if(this->GetCurDoorOpenSide() != rParamBase.GetCurDoorOpenSide())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetCurDoorOpenSide";
        return true;
    }

    if(this->GetLineId() != rParamBase.GetLineId())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetLineId";
        return true;
    }

    if(this->GetRunDir() != rParamBase.GetRunDir())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetRunDir";
        return true;
    }

//    if(this->GetDevLoaction() != rParamBase.GetDevLoaction())
//    {
//        LOG_QDEBUG << "IsParamNeedUpdate GetDevLoaction" << this->GetDevLoaction() << rParamBase.GetDevLoaction();
//        return true;
//    }

    if(DEFAULTVAL != rParamBase.GetDoorIsolation() &&
            this->GetDoorIsolation() != rParamBase.GetDoorIsolation())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetDoorIsolation";
        return true;
    }


    if(this->GetLoactionIp() != GetLoactionIp())
    {
        LOG_QDEBUG << "IsParamNeedUpdate GetLoactionIp";
        return true;
    }

    return false;

}








/**
 * @brief ParamBase::CalCurDevLocation
 * 通用获取设备位置信息接口
 */
void ParamBase::CalCurDevLocation()
{
    int declocation = TONGXING_IP_LEFT;
    //根据 ip 得到当前的 设备位置
    LOG_QDEBUG << GetLoactionIp();
    int lastOneByte = GetLoactionIp().right(1).toInt();

    switch(lastOneByte%2)
    {
        case 0:
            declocation = TONGXING_IP_RIGHT;
            LOG_QDEBUG<< "INSTALL_RIGHT";
        break;
        case 1:
            declocation = TONGXING_IP_LEFT;
            LOG_QDEBUG<< "INSTALL_LEFT";
        break;
        default:
        break;
    }
    switch(GetCarHead())
    {
        case TONGXING_KEY_POS_NONE:

        break;
        case TONGXING_KEY_POS_1:
            if(TONGXING_IP_RIGHT == declocation)
            {
                declocation = TONGXING_IP_LEFT;
            }
            else if(TONGXING_IP_LEFT == declocation)
            {
                declocation = TONGXING_IP_RIGHT;
            }
        break;
        case TONGXING_KEY_POS_6:
            if(TONGXING_IP_RIGHT == declocation)
            {
                declocation = TONGXING_IP_RIGHT;
            }
            else if(TONGXING_IP_LEFT == declocation)
            {
                declocation = TONGXING_IP_LEFT;
            }
        break;
        case TONGXING_KEY_POS_ALL:

        break;
        default:
        break;

    }
    SetDevLoaction(declocation);

}



/**
 * @brief ParamBase::GetLocalIp
 * 根据网口名 获取当前ip
 * @param netDevName
 * @return
 */
const QString ParamBase::GetLocalIp(const QString& rNetDevName)
{
    QList<QNetworkInterface> interfaceList = QNetworkInterface::allInterfaces();
    foreach(QNetworkInterface interfaceItem, interfaceList)
    {
        if(interfaceItem.humanReadableName().compare(rNetDevName, Qt::CaseInsensitive)==0)
        {
            QList<QNetworkAddressEntry> addressEntryList = interfaceItem.addressEntries();
            foreach(QNetworkAddressEntry addressEntryItem, addressEntryList)
            {
                if(QAbstractSocket::IPv4Protocol == addressEntryItem.ip().protocol())
                {
                    return addressEntryItem.ip().toString();
                }
            }
        }
    }
    return NULL;
}

